package org.ims.inv.service.project.impl;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.ims.inv.dao.project.DAOProject;
import org.ims.inv.dao.project.DAOProjectEvent;
import org.ims.inv.model.project.ModelProject;
import org.ims.inv.model.project.ModelProjectEvent;
import org.ims.inv.service.project.ServiceProjectEvent;
import org.ims.inv.web.util.ContextUtil;

import cn.trymore.core.exception.DAOException;
import cn.trymore.core.exception.ServiceException;
import cn.trymore.core.service.impl.ServiceGenericImpl;
import cn.trymore.core.util.UtilString;

/**
 * Service: 项目操作记录
 * 
 * @author Jeccy.Zhao
 *
 */
public class ServiceProjectEventImpl
extends ServiceGenericImpl<ModelProjectEvent> implements ServiceProjectEvent
{

	/**
	 * Repository of project
	 */
	private DAOProjectEvent daoProjectEvent;
	
	/**
	 * Repository of project
	 */
	private DAOProject daoProject;
	
	public ServiceProjectEventImpl(DAOProjectEvent dao)
	{
		super(dao);
		
		this.daoProjectEvent = dao;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.ims.inv.service.project.ServiceProjectEvent#recordEvent(org.ims.inv.model.project.ModelProject, java.lang.Integer, java.lang.String)
	 */
	@Override
	public void traceEvent (ModelProject project, 
			String eventName, Integer eventState, 
			String eventComments) throws ServiceException
	{
		try
		{
			if (project != null && UtilString.isNotEmpty(project.getId()))
			{
				ModelProjectEvent event = new ModelProjectEvent();
				
				event.setProject(project);
				event.setEventName(eventName);
				event.setEventTime(new Date());
				event.setEventOperator(ContextUtil.getCurrentUser());
				event.setEventState(eventState);
				event.setEventComments(eventComments);
				
				this.daoProjectEvent.saveOrUpdate(event);
				
				// 更新关联
				Set<ModelProjectEvent> events  = project.getProcessEvents();
				if (events == null)
				{
					project.setProcessEvents(new HashSet<ModelProjectEvent>());
				}
				project.getProcessEvents().add(event);
				
			}
		}
		catch (DAOException e)
		{
			throw new ServiceException(e);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see cn.trymore.core.service.impl.ServiceGenericImpl#getCriterias(cn.trymore.core.model.ModelBase)
	 */
	@Override
	protected DetachedCriteria getCriterias(ModelProjectEvent domain)
	{
		DetachedCriteria criteria = DetachedCriteria.forClass(ModelProjectEvent.class);
		
		if (domain != null)
		{
			if (domain.getProject() != null)
			{
				criteria.createCriteria("project").add(
						Restrictions.eq("id", domain.getProject().getId()));
			}
			
			if (UtilString.isNotEmpty(domain.getEventComments()))
			{
				criteria.add(Restrictions.eq("eventComments", domain.getEventComments()));
			}
			
			if (domain.getEventState() != null)
			{
				criteria.add(Restrictions.eq("eventState", domain.getEventState()));
			}
			
			if (UtilString.isNotEmpty(domain.getEventName()))
			{
				criteria.add(Restrictions.eq("eventName", domain.getEventName()));
			}
			
		}
		
		return criteria;
	}

	public void setDaoProjectEvent(DAOProjectEvent daoProjectEvent)
	{
		this.daoProjectEvent = daoProjectEvent;
	}

	public DAOProjectEvent getDaoProjectEvent()
	{
		return daoProjectEvent;
	}
	
	public DAOProject getDaoProject()
	{
		return daoProject;
	}

	public void setDaoProject(DAOProject daoProject)
	{
		this.daoProject = daoProject;
	}

}
